
var nametxt = $('.slot');
var phonetxt = $('.name');
var pcount ;//参加人数
var runing = true;
var trigger = true;
// var inUser = (Math.floor(Math.random() * 10000)) % 5 + 1;
var inUser = 999999;//没有指定中奖人
var num = 0;
var Lotterynumber = 10; //设置单次抽奖人数
var l=10;

var xinm = new Array();
var phone = new Array();
var openid= new Array();
op();

function op() {
    $.ajax({
        url:"../activity/xinm",
        type:"post",
        success:function (data) {
            // console.log(data)
            for(var p in data){//遍历json对象的每个key/value对,p为key
                phone[p] = data[p].nickname;
                xinm [p] =data[p].headimgurl;
                openid [p] =data[p].openid;
//                console.log(p + " " + data[p].headimgurl);
//             console.log(p)
            }
            pcount=xinm.length-1;//参与人数
            nametxt.css('background-image','url(/game-activity/img/10.png)');
            phonetxt.html("名称");
            $(".ppp").html(pcount)
        },
        error:function () {

        }
    });
}

// console.log(xinm);
// console.log(phone)
var v=true;

//循环定时器
intervalId = setInterval(function () {
    op();
    console.log("刷新参与人数ing~");
    // clearInterval(intervalId);
}, 1000);


var ii=0;


// 开始停止
function start() {
	//终止刷新参与人数
    clearInterval(intervalId);
    console.log("终止刷新");
   $(".luck-user-list").html("");
	if (runing) {
		if ( pcount <= Lotterynumber ) {
			alert("抽奖人数不足"+Lotterynumber+"人");
		}else{
			runing = false;
			$('#start').text('停止');
			startNum();
		}
	} else {
		$('#start').text('自动抽取中('+ Lotterynumber+')');
		zd();

	}

}

// 开始抽奖

function startLuck() {
	runing = false;
	$('#btntxt').removeClass('start').addClass('stop');
	startNum()
}

// 循环参加名单
function startNum() {
	num = Math.floor(Math.random() * pcount);
	nametxt.css('background-image','url('+xinm[num]+')');
	phonetxt.html(phone[num]);
	t = setTimeout(startNum, 0);
}

// 停止跳动
function stop() {
	pcount = xinm.length-1;
	clearInterval(t);
	t = 0;
}


// 打印中奖人
function zd() {
	if (trigger) {

		trigger = false;
		var i = 0;
		console.log(pcount);
		console.log(Lotterynumber)
		if ( pcount >= Lotterynumber ) {
			stopTime = window.setInterval(function () {
				if (runing) {
					
					runing = false;
					$('#btntxt').removeClass('start').addClass('stop');
					startNum();
				} else {
					runing = true;
					$('#btntxt').removeClass('stop').addClass('start');
					stop();

					i++;
					Lotterynumber--;

					$('#start').text('自动抽取中('+ Lotterynumber+')');

					if ( i == l) {
						console.log("抽奖结束");
						window.clearInterval(stopTime);
						$('#start').text("开始");
						Lotterynumber = l;
						trigger = true;
					};

					if ( Lotterynumber == inUser) { 
						// 指定中奖人
						nametxt.css('background-image','url(img/7.jpg)');
						phonetxt.html('指定中奖人');
						$("<li><div class='portrait' style='background-image:url(/game-activity/img/7.jpg)'></div><div class='luckuserName'>指定中奖人</div></li>").appendTo('.luck-user-list');
						$("<li><div class='luck-img' style='background-image:url(/game-activity/img/7.jpg)'></div><p>指定中奖人</p></li>").appendTo('.modality-list ul');
						inUser = 9999;
					}else{
						//打印中奖者名单
						$("<li><div class='portrait' style='background-image:url("+xinm[num]+")'></div><div class='luckuserName'>"+phone[num]+"</div></li>").appendTo(".luck-user-list");
						$("<li><div class='luck-img' style='background-image:url("+xinm[num]+")'></div><p>"+phone[num]+"</p></li>").appendTo(".modality-list ul");

                        $.ajax({
                            url:"../activity/stockpile",
                            data:{
                            	openid:openid[num],
								size:ii
                            },
                            type:"post",
							success:function () {
								console.log("存入成功")
                            },
							error:function () {
								console.log("存入失败")
                            }
                        });
                        if(l==i){
                            ii++;
                            console.log("i++");
						}
						//将已中奖者从数组中"删除",防止二次中奖
						xinm.splice($.inArray(xinm[num], xinm), 1);
						phone.splice($.inArray(phone[num], phone), 1);
					};
				}
			},500);
		};
	}
}

//点击二维码放大
$(".erweima img").on("click",function(){
	$("#alert_mask").show();
});

$("#alert_mask").on("click",function(){
	$("#alert_mask").hide();
});
















